﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using PreluareDateDePeEBay.eBayService;
using PreluareDateDePeEBay.tradingApi;

namespace PreluareDateDePeEBay
{
    class Program
    {
        public enum TipRequest { KeyWords, Categorie }
        static void Main(string[] args)
        {
            var tip = TipRequest.Categorie;
            using (FindingServicePortTypeClient client = new FindingServicePortTypeClient())
            {
                MessageHeader header = MessageHeader.CreateHeader("CustomHeader", "", "");
                using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
                {

                    OperationContext.Current.OutgoingMessageHeaders.Add(header);

                    HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();

                    httpRequestProperty.Headers.Add("X-EBAY-SOA-SECURITY-APPNAME", "FilipSin-b22f-4d3d-90c6-68eb75bf2db2");  // Acest ultim argument l-am luat de pe contul de eBay Developer de la Application Settings
                    //httpRequestProperty.Headers.Add("X-EBAY-SOA-SECURITY-APPNAME", "EricMass-d56b-4d34-bf83-8f54ca540f97"); am mai gasit unu pe net, in cazul in care ne luam ban
                    switch (tip)
                    {
                        case TipRequest.KeyWords: httpRequestProperty.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "findItemsByKeywords"); break;
                        case TipRequest.Categorie: httpRequestProperty.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "findItemsByCategory"); break;
                    }
                    httpRequestProperty.Headers.Add("X-EBAY-SOA-GLOBAL-ID", "EBAY-US");

                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                    
                    switch (tip)
                    {
                        case TipRequest.KeyWords:
                            {
                                FindItemsByKeywordsRequest request = new FindItemsByKeywordsRequest();
                                request.keywords = "Eliade";

                                FindItemsByKeywordsResponse response = client.findItemsByKeywords(request);

                                foreach (var item in response.searchResult.item)
                                {
                                    Console.WriteLine(item.title + "%%%" + item.primaryCategory.categoryId);
                                }
                            } break;
                        case TipRequest.Categorie:
                            {
                                FindItemsByCategoryRequest req = new FindItemsByCategoryRequest() { categoryId = new string[] { "377" } };
                                PaginationInput pi = new PaginationInput();
                                pi.entriesPerPage = 200;
                                pi.entriesPerPageSpecified = true;
                                pi.pageNumberSpecified = true;
                                pi.pageNumber = 1;

                                req.paginationInput = pi;

                                FindItemsByCategoryResponse res = client.findItemsByCategory(req);

                                foreach (var v in res.searchResult.item)
                                    Console.WriteLine(v.title);
                            } break;
                    }

                    Console.Read();
                }
            }
        }
    }
}